#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import os
from gitlab_map import site_map
import config as c


def git_push(filepath, git_flow_open):
    domain_list = c.dirname_list
    gitflow = c.gitflow_list
    domain = []

    if git_flow_open:
        for i in domain_list:
            for k in gitflow:
                if k == i:
                    domain.append(k)
    else:
        for i in domain_list:
            for k in gitflow:
                if k == i:
                    domain_list.remove(k)
    domain = domain_list

    for i in domain:
        name = i.lower()
        print(name)
        fi = site_map[name]
        fi_d = filepath
        try:
            if git_flow_open:
                f = fi.split('/')[1][:-4]
                cmd = "cd /d %s%s && git checkout master && git merge hotfix/%s && git push && git checkout develop && git pull && git merge master && git push" % (fi_d, f, c.hot_fix_name)
                print("cmd=", cmd)
                os.system(cmd)
                print("-------------gitflow done--------------")
            else:
                f = fi.split('/')[1][:-4]
                cmd = "cd /d && git push"
                print("cmd=", cmd)
                os.system(cmd)
                print("-------------no gitflow done--------------")
        except:
            print(fi + ' ***** 推送失败 *****')


if __name__ == "__main__":
    batch_git_path = c.batch_git_path
    git_push(batch_git_path, c.git_flow_check)